// pages/waitMe/waitMe.js
import {
  API
} from "../../utils/const"
import {
  formatTime
} from "../../utils/dateUtils"
import {
  getRequest,
  postRequest
} from "../../utils/request"
import {
  checkCount
} from "../../utils/formCheckUtil"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    app: app,
    waitConfirmList: [],
    disinfectCount: 0
  },
  bindSearchInput(e) {
    console.log(e);
    var val = e.detail.value.trim();
    this.setData({
      searchStr: val
    })
    this.waitConfirmGroup(val)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onEquipBeUse: function (e) {
    const {
      op,
      item
    } = e.currentTarget.dataset;
    console.log("item", item)
    const that = this;
    const maxEnable = item.equipCount;
    wx.showModal({
      title: `消毒完成器械：${item.equipName}`,
      editable: true,
      confirmText: '消毒完成',
      placeholderText: `输入完成数量,可完成${maxEnable}个`,
      // content:1
      success: (res) => {
        if (res.confirm) {

          const count = checkCount(res.content)
          if (count > 0) {
            const params = {
              count,
              type: op,
              equipName: item.equipName
            }
            that.equipUse(params).then(res => {
              that.onShow(that.data.searchStr);
            });
          } else {
            that.onShow(that.data.searchStr);
          }

        }
      }
    })

  },
  equipUse(params) {
    return postRequest(API.equipChange, {
      ...params
    }).then(res => {
      wx.hideLoading();
      if (res) {
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 1000,
        })
      }
    })
  },
  waitConfirmGroup: function (searchStr) {
    getRequest(API.waitConfirmGroup, {
      equipStatus: "DISINFECTING",
      searchStr: searchStr ? searchStr : ''
    }).then(res => {
      const dataList = [];
      Object.keys(res).forEach(key => {
        console.log("res[key]", res[key])
        Object.keys(res[key]).forEach(equip => {
          console.log("equip", equip)
          const data = {}
          data.nickName = JSON.parse(key).nickName;
          data.equipName = equip;
          data.equipCount = res[key][equip];
          // data[JSON.parse(key).nickName] = res[key];
          dataList.push(data);
        });
      })
      this.setData({
        waitConfirmList: dataList
      })
    })
  },
  doListQuery: function (e) {
    getRequest(API.waitConfirmGroup, {
      equipStatus: "DISINFECTING",
      ...e.detail
    }).then(res => {
      const dataList = [];
      Object.keys(res).forEach(key => {
        console.log("res[key]", res[key])
        Object.keys(res[key]).forEach(equip => {
          console.log("equip", equip)
          const data = {}
          data.nickName = JSON.parse(key).nickName;
          data.equipName = equip;
          data.equipCount = res[key][equip];
          // data[JSON.parse(key).nickName] = res[key];
          dataList.push(data);
        });
      })
      this.setData({
        waitConfirmList: dataList
      })
    })
  },
  onConfirmClick: function () {
    that.setData({
      showInput: true
    })
  },

  myUsingList: function () {
    getRequest(API.equipUsingList).then(res => {
      this.setData({
        usingEquipList: res
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  fetchDisfinctCount: function (params) {
    {
      getRequest(API.waitConfirmGroupCount, params).then(res => {
        this.setData({
          disinfectCount: res.count
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      searchStr: null
    })
    this.waitConfirmGroup();
    this.fetchDisfinctCount({
      equipStatus: 'DISINFECTING'
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})